package com.cnc.dao.Impl;

import com.cnc.dao.StudentDao;
import com.cnc.entity.Student;
import com.cnc.utils.DBUtil;
import com.cnc.utils.PageUtil;
import com.cnc.utils.SearchUtil;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.servlet.annotation.WebFilter;
import java.sql.SQLException;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    public List<Student> getAllStudent() {
        List<Student> list = null;
        try {
            list = DBUtil.getQr().query("select * from student", new BeanListHandler<>(Student.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int deleteStudent(int id) {
        int rows = 0;
        try {
            rows = DBUtil.getQr().update("delete from student where sid=?", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }

    public int insertStudent(Student s) {
        int rows = 0;
        try {
            rows = DBUtil.getQr().update("insert into student values(?,?,?,?,?,?)",s.getSid(),s.getSname(),s.getSage(),s.getSgender(),s.getSemail(),s.getPhoto());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }

    public int updateStudent(int Sid,String field,String value) {
        int rows = 0;
        try {
            rows = DBUtil.getQr().update("update student set " + field + "=? where sid=?",value,Sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int getCountRows(SearchUtil su) {
        int rows = 0;
        String sql = "select count(*) from student where sname like ? AND sage>=? AND sage<=? AND sgender like ?";
        try {
            rows = (int)(long)DBUtil.getQr().query(sql, new ScalarHandler(),su.getOpt_name(),su.getMin(),su.getMax(),su.getGender());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public List<Student> getStudentByPage(PageUtil pu) {
        List<Student> list = null;
        String sql = "select * from student limit ?,?";
        try {
            list = DBUtil.getQr().query(sql, new BeanListHandler<>(Student.class), pu.getIndex(), pu.getRows());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getMaxId() {
        int maxId = 0;
        try {
            maxId = (int) DBUtil.getQr().query("select Max(sid) from student", new ScalarHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return maxId;
    }

    @Override
    public Student getStudentBySid(int sid) {
        Student s = null;
        try {
            s = DBUtil.getQr().query("select * from student where sid=?", new BeanHandler<>(Student.class), sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return s;
    }

    @Override
    public int editStudent(Student s) {
        int rows = 0;
        try {
            rows = DBUtil.getQr().update("update student set sname=?,sage=?,sgender=?,semail=?,photo=? where sid=?", s.getSname(), s.getSage(), s.getSgender(), s.getSemail(), s.getPhoto(), s.getSid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }

    public List<Student> searchStudent(SearchUtil su, PageUtil pu){
        List<Student> list = null;
        String sql="select * from student where sname like ? AND sage>=? AND sage<=? AND sgender like ? limit ?,?";
        try {
            list = DBUtil.getQr().query(sql, new BeanListHandler<>(Student.class),su.getOpt_name(),su.getMin(),su.getMax(),su.getGender(),pu.getIndex(), pu.getRows());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
